//
//  PickDateTimeCell.m
//  Remind
//
//  Created by zhufeng on 2017/10/24.
//  Copyright © 2017年 2017.09.27. All rights reserved.
//

#import "PickDateTimeCell.h"

NSString * const  XLFormRowDescriptorTypePickDateAndTime = @"XLFormRowDescriptorTypePickDateAndTime";

@interface PickDateTimeCell ()

@end

@implementation PickDateTimeCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    //[self configure];
}

+ (void)load{
    [XLFormViewController.cellClassesForRowDescriptorTypes setObject:NSStringFromClass([PickDateTimeCell class]) forKey:XLFormRowDescriptorTypePickDateAndTime];
}

- (void)configure{
    [super configure];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    CGRect rect = [UIScreen mainScreen].bounds;
    UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, rect.size.width, 244)];
    
    UIDatePicker *pickView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, rect.size.width, 200)];
    pickView.datePickerMode = UIDatePickerModeDateAndTime;
    pickView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [pickView addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 201, rect.size.width, 1)];
    line.backgroundColor = [UIColor grayColor];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, rect.size.width, 44)];
    lineView.backgroundColor = [UIColor whiteColor];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(rect.size.width - 58, 0, 50, 44);
    [btn setTitle:@"Done" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor colorWithRed:44/255.0 green:124/255.0 blue:246/255.0 alpha:1.0] forState:UIControlStateNormal];
    btn.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
    [btn addTarget:self action:@selector(done) forControlEvents:UIControlEventTouchUpInside];
    
    [lineView addSubview:btn];
    [inputView addSubview:pickView];
    [inputView addSubview:line];
    [inputView addSubview:lineView];

#pragma mark - config
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];// 创建一个日期格式器
    [formatter setDateFormat:@"yyyy-MM-dd-HH:mm"];
    NSString *dateString = [formatter stringFromDate:date];
    self.timeTextField.text = dateString;
    self.timeTextField.textColor = [UIColor lightGrayColor];
    self.timeTextField.inputView = inputView;
    self.timeTextField.textAlignment = NSTextAlignmentRight;
    
}

- (void)done{
    [self.timeTextField resignFirstResponder];
}

- (void)dateChange:(id)sender{
    UIDatePicker *pick = sender;
    NSDate *pickerDate = [pick date];
    NSDateFormatter *pickerFormatter = [[NSDateFormatter alloc] init];// 创建一个日期格式器
    [pickerFormatter setDateFormat:@"yyyy-MM-dd-HH:mm"];
    NSString *dateString = [pickerFormatter stringFromDate:pickerDate];
    
    //NSLog(@"格式化显示时间：%@",dateString);
    self.timeTextField.text = dateString;
    self.rowDescriptor.value = self.timeTextField.text;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
